package idv.jieh.language.struts;

import javax.servlet.http.*;

import idv.jieh.web.*;
import org.apache.struts.action.*;
import org.apache.struts.Globals;

public class LanguageCtrl extends StrutsCtrl {

  public LanguageCtrl() {
    super.disableSecurity();
  }

  protected ActionForward process(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response,
    ActionMessages messages)
  throws Exception {
    // 測試動態變更語系
    String language = idv.jieh.commons.util.DataAnalysis.trimNull(request.getParameter("language"));
    java.util.Locale locale
      = language.equals("US")
        ? java.util.Locale.US : language.equals("CHINA")
        ? java.util.Locale.CHINA : language.equals("JAPAN")
        ? java.util.Locale.JAPAN : java.util.Locale.TAIWAN;

    HttpSession session = request.getSession(true);
    session.setAttribute(org.apache.struts.Globals.LOCALE_KEY, locale);

    // 測試錯誤訊息引用多國語系資源檔
    messages.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("demo.errormsg"));

    // 測試動態取得多國語系資源檔文字內容
    MyMessageResources message = (MyMessageResources)session.getServletContext().getAttribute(Globals.MESSAGES_KEY);
    System.out.println(message.getMessage(locale, "demo.hello"));

    return mapping.findForward("LanguageDemo");
  }

}
